﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Practices.Unity.Interception
{
    class AssertEx
    {
       public delegate void ThrowsDelegate();

        public static T Throws<T>(ThrowsDelegate testCode)
            where T : Exception
        {
            return (T)Throws(typeof(T), testCode);
        }

        public static T Throws<T>(string userMessage,
                                  ThrowsDelegate testCode)
            where T : Exception
        {
            return (T)Throws(typeof(T), testCode);
        }

        public static Exception Throws(Type exceptionType,
                                       ThrowsDelegate testCode)
        {
            Exception exception = null;

            try
            {
                testCode();
            }
            catch (Exception thrownException)
            {
                exception = thrownException;
            }

            if (exception == null)
                Assert.Fail("Expected {0} but no exception was thrown.", exceptionType.Name);

            if (!exceptionType.Equals(exception.GetType()))
                Assert.Fail("Expected {0} but was {1}", exceptionType, exception);

            return exception;
        }
    }
}
